from django.http import HttpResponse,HttpResponseRedirect
from evaluate.models  import StudentEvaluateEnvironment as advice #学生意见反馈
from evaluate.models import StudentHot,Function,StudentEnvironmentAdvice
from evaluate.models import StudentEvaluateOrganizaton,TeacherEvaluateOrganization,OrganizationHot
from collections import Counter
import jieba,jieba.analyse
import os,sys

#分析学生评价校园环境热词
def make_hot_word(request):
	term = Function.objects.filter(state="end").order_by('-id')[:1][0]
	student_hot = StudentHot.objects.filter(year_term=term.year_term) 
	if student_hot:  #判断是否计算过
		pass
	else: 
		path = os.path.dirname(__file__)
		jieba.load_userdict(path+"\\school.txt") #添加生产环境的词典
		advice_list = advice.objects.all()
		teaching = ""
		accommodation = ""
		diet = ""
		totality = ""
		for v in advice_list:
			teaching = teaching + v.teaching_environment_proposal
			accommodation = accommodation + v.accommodation_environment_proposal
			diet = diet + v.diet_environment_proposal
			totality = totality + v.totality_proposal
		tags_teaching = jieba.analyse.extract_tags(teaching,topK=6)
		tags_accommodation = jieba.analyse.extract_tags(accommodation,topK=6)
		tags_diet = jieba.analyse.extract_tags(diet,topK=6)
		tags_totality = jieba.analyse.extract_tags(totality,topK=6)
		re_teaching = "+".join(tags_teaching)
		re_accommodation = "+".join(tags_accommodation)
		re_diet = "+".join(tags_diet)
		re_totality = "+".join(tags_totality)
		word_list_teaching = re_teaching.split("+")
		word_list_accommodation = re_accommodation.split("+")
		word_list_diet = re_diet.split("+")
		word_list_totality = re_totality.split("+")
		StudentHot.objects.create(year_term=term.year_term,environment_type="teaching",word=re_teaching,value=count_word(word_list_teaching,teaching))
		StudentHot.objects.create(year_term=term.year_term,environment_type="accommodation",word=re_accommodation,value=count_word(word_list_accommodation,accommodation))
		StudentHot.objects.create(year_term=term.year_term,environment_type="totality",word=re_totality,value=count_word(word_list_totality,totality))
		StudentHot.objects.create(year_term=term.year_term,environment_type="diet",word=re_diet,value=count_word(word_list_diet,diet))
		hot_advice(term.year_term,"teaching",word_list_teaching)
		hot_advice(term.year_term,"accommodation",word_list_accommodation)
		hot_advice(term.year_term,"diet",word_list_diet)
		hot_advice(term.year_term,"totality",word_list_totality)
	return HttpResponse("成功")


#计算热词出现次数
def  count_word(word_list,content):
	word_vule=""
	for v in word_list:  
		word_vule = word_vule + str(content.count(v)) + ","
	word_vule = word_vule[:len(word_vule)-1]
	return word_vule


#分析出热点反馈
def hot_advice(year_term,ty,word_list):
	content = ""
	advice_list = advice.objects.all()
	if ty == "teaching":
		for key in word_list:
			num = 0
			for v in advice_list:
				if	v.teaching_environment_proposal.find(key)>=0:
					content = content + v.teaching_environment_proposal + "#"
					num+=1
				if num == 5 :
					break
	elif ty == "accommodation":
		for key in word_list:
			num = 0
			for v in advice_list:
				if	v.accommodation_environment_proposal.find(key)>=0:
					content = content + v.accommodation_environment_proposal + "#"
					num+=1
				if num == 5 :
					break
	elif ty == "diet":
		for key in word_list:
			num = 0
			for v in advice_list:
				if	v.diet_environment_proposal.find(key)>=0:
					content = content + v.diet_environment_proposal + "#"
					num+=1
				if num == 5 :
					break
	elif ty == "totality":
		for key in word_list:
			num = 0
			for v in advice_list:
				if	v.totality_proposal.find(key)>=0:
					content = content + v.totality_proposal + "#"
					num+=1
				if num == 5 :
					break
	content = content[:len(content)-1]
	StudentEnvironmentAdvice.objects.create(year_term=year_term,environment_type=ty,advice=content)


# #提取对组织机构的热词
# def get_organization_hot_word():
# 	term = Function.objects.filter(state="end").order_by('-id')[:1][0]
# 	student_hot = StudentHot.objects.filter(year_term=term.year_term) 
# 	if student_hot:  #判断是否计算过
# 		pass
# 	else: 
# 		path = os.path.dirname(__file__)
# 		jieba.load_userdict(path+"\\school.txt") #添加生产环境的词典